package data;

import java.io.File;

import org.apache.log4j.Logger;

import core.Core;

public abstract class DataManager {
	
	protected Logger log=Core.log;
	protected Logger warningsLog=Core.warningsLog;

	protected boolean[] pieces;
	protected int pieceLength;
	protected int lastPieceLength;
	protected String MD5;
	protected File file;
	protected boolean downloadComplete=false;
	
	

	public File getFile() {
		return file;
	}	
	
	public abstract void checkPieces();
	public abstract void write(byte[] data,int indPiece);
	public abstract byte[] read(int indPiece);
	public abstract boolean checkMD5();
	
	public boolean isDownloadComplete(){
		
		if (downloadComplete) return true;
		
		downloadComplete=true;
		int i=0;
		while (i<pieces.length && downloadComplete){
			if (!pieces[i]) downloadComplete=false;
			i++;
		}
		return downloadComplete;
		
	}	
	
	
	public synchronized boolean[] getPieces() {
		return pieces.clone();
	}
	public void setPieces(boolean[] pieces) {
		this.pieces = pieces;
	}
	public synchronized int  getPieceLength() {
		int ret = pieceLength;
		return ret;
		
	}
	public void setPieceLength(int pieceLength) {
		this.pieceLength = pieceLength;
	}
	public synchronized int getLastPieceLength() {
		int ret = lastPieceLength;
		return ret;
	}
	public void setLastPieceLength(int lastPieceLength) {
		this.lastPieceLength = lastPieceLength;
	}

}
